package com.xbongbong.executor.command;

import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.parent.interfaces.FormDataUpdateService;
import com.xbongbong.pro.constant.I18nStringConstant;
import com.xbongbong.pro.enums.SaasMarkEnum;
import com.xbongbong.pro.enums.errorcodes.SystemErrorCodeEnum;
import com.xbongbong.saas.constant.ParameterConstant;
import com.xbongbong.saas.enums.XbbRefTypeEnum;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Component;

import java.util.Map;
import java.util.Objects;

/**
 * FormDataUpdateService 工厂
 * @author 吴峰
 * @date 2020/12/30 15:27
 * @since
 * @version
 */
@Component
public class FormDataUpdateServiceFactory {

    private static final Logger LOG = LoggerFactory.getLogger(FormDataUpdateServiceFactory.class);


    @Autowired
    @Lazy
    private Map<Integer, FormDataUpdateService> businessUpdateMap;
    /**
     * 工厂
     * @param businessType
     * @param saasMark
     * @return
     */
    public FormDataUpdateService getFormDataUpdateService(Integer businessType, Integer saasMark) throws XbbException {
        FormDataUpdateService formDataUpdateService = null;
        if (Objects.equals(SaasMarkEnum.SAAS.getCode(), saasMark)) {
            formDataUpdateService = businessUpdateMap.get(businessType);
        } else if (Objects.equals(SaasMarkEnum.PAAS.getCode(), saasMark)){
            formDataUpdateService = businessUpdateMap.get(XbbRefTypeEnum.PAAS.getCode());
        }
        if (Objects.isNull(formDataUpdateService)) {
            LOG.error("FormDataUpdateServiceFactory.getFormDataUpdateService：businessType="+businessType, "； saasMark="+saasMark);
            throw new XbbException(SystemErrorCodeEnum.API_ERROR_100001, I18nStringConstant.NOT_EXIST, ParameterConstant.BUSINESS_TYPE);
        }
        return formDataUpdateService;
    }
}
